package com.hzlx.service;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;


import com.hzlx.dao.BendingRepository;
import com.hzlx.dao.CuttingMachineStatusDao;
import com.hzlx.dao.TemperatureRepository;
import com.hzlx.entity.BendingMachineData;

import com.hzlx.entity.CuttingMachineStatus;
import com.hzlx.entity.TemperatureData;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class MqttMessageService {
    @Resource
    private TemperatureRepository temperatureRepository;
    @Resource
    private BendingRepository bendingRepository;
    @Resource
    private CuttingMachineStatusDao cuttingMachineStatusDao;
    private final ObjectMapper objectMapper = new ObjectMapper();

    public void processAndSaveMessage(String payload) {
        try {
            JsonNode jsonNode = objectMapper.readTree(payload);
            String group = jsonNode.get("group").asText();

            switch (group) {
                case "tempeture":
                    processTemperatureMessage(jsonNode);
                    break;
                case "cutting-machine":
                    processCuttingMachineMessage(jsonNode);
                    break;
                case "bending-machine":
                    processBendingMachineMessage(jsonNode);
                    break;
                default:
                    System.out.println("未知的消息组: " + group);
            }
        } catch (Exception e) {
            System.err.println("处理PLC消息失败: " + e.getMessage());
            e.printStackTrace();
        }
    }

    private void processBendingMachineMessage(JsonNode jsonNode) {
        JsonNode values = jsonNode.get("values");
        BendingMachineData bendingMachineData = new BendingMachineData();
        bendingMachineData.setBendingAngle(values.get("bending-angle").floatValue());
        bendingMachineData.setCircleCount(values.get("circle-count").intValue());
        bendingMachineData.setBendingPressure(values.get("bending-pressure").floatValue());
        bendingMachineData.setNumberDate(new Date());
        bendingRepository.save(bendingMachineData);
        System.out.println("折弯机保存成功: " + bendingMachineData);
    }

    private void processTemperatureMessage(JsonNode jsonNode) {
        Float temperature = jsonNode.get("values").get("tempeture").floatValue();
        TemperatureData data = new TemperatureData();
        data.setTemperature(temperature);
        data.setNumberDate(new Date());
        temperatureRepository.save(data);
        System.out.println("温度数据保存成功: " + data);
    }

    private void processCuttingMachineMessage(JsonNode jsonNode) {
        JsonNode values = jsonNode.get("values");
        CuttingMachineStatus status = new CuttingMachineStatus();
        status.setGroupName(jsonNode.get("group").asText());
        status.setNode(jsonNode.get("node").asText());
        status.setCutCount(values.get("cut-count").intValue());
        status.setCutSpeed((double) values.get("cut-speed").floatValue());
        status.setCutLife(values.get("cut-life").intValue());
        status.setEngineOverload(values.get("engine-overload").intValue());
        status.setTimestamp(new Date(jsonNode.get("timestamp").longValue()).getTime());
        status.setCreatedAt(new Date());
        cuttingMachineStatusDao.save(status);
        System.out.println("切割机状态保存成功: " + status);
    }
}
